import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { REQ_URL } from '../Constants'
import { HttpService } from '../httpService'
// import { AppGlobal } from '../appGlobal'

/*
  Generated class for the UserProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class MsgProvider {

    constructor(public http: Http,
        public httpService: HttpService) {
        //console.log('Hello UserProvider Provider');
    }

    //获取消息列表
    getMsgList(obj?: any) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.msg.getList, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }


    //获取消息列表
    getMsgDetail(id) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.msg.getDetail + "/" + id)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //获取消息列表
    clearMsgAll() {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.msg.clear)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //获取未读消息数量
    getUnReadMsgNum(obj?: any) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.msg.unReadNum, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //标记为已读消息
    markReadMsg(id) {
        var obj = {
            messageId: id
        }
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.msg.markRead, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

}
